import os

# 1. 构建课程-教师映射字典
course_teacher_map = {}
teacher_file = "./01-exercise/01-20/course_teacher.txt"

with open(teacher_file, 'r', encoding='utf-8') as fin:
    for line in fin:
        line = line.rstrip('\r\n')  # 安全去除换行符
        course, teacher = line.split(",")
        course_teacher_map[course] = teacher

# 2. 处理学生成绩数据
grade_file = "./01-exercise/01-20/course_student_grade_input.txt"
course_grades = {}  # 初始化课程成绩字典

with open(grade_file, 'r', encoding='utf-8') as fin:
    for line in fin:
        line = line.rstrip('\r\n')  # 安全去除换行符
        course, sno, sname, sgrade = line.split(",")
        
        # 获取教师信息
        teacher = course_teacher_map.get(course, "未知教师")
        
        # 打印关联信息
        print(f"{course}, {teacher}, {sno}, {sname}, {sgrade}")
        
        # 存储到成绩字典（可选）
        if course not in course_grades:
            course_grades[course] = []
        course_grades[course].append(float(sgrade))  # 转换为浮点数

# 3. 简单统计输出
print("\n课程成绩统计:")
for course, grades in course_grades.items():
    avg = sum(grades) / len(grades)
    print(f"{course}: 平均分={avg:.2f}")